package com.health.activity.jkjy;

import com.health.NovaActivity;
import com.health.NovaApplication;
import com.health.R;
import com.health.tablebean.ARCHIVE_BASEINFO;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

/**
 * 播放视频
 * 
 * @author nova
 *
 */
public class PlayVideoActivity extends NovaActivity
		implements MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
	// private TextView tv_name;
	
	private ARCHIVE_BASEINFO userinfo = null;
	private String uri = "";
	private String flag = "";
	private Button btn_back;
	private Button btn_up;
	private int mPositionWhenPaused = -1;
	private MediaController mMediaController;
	private VideoView vv;
	
	private TextView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jkjy_playvideo);
		userinfo = NovaApplication.getUserInfo();
		
		// Create media controller
		mMediaController = new MediaController(this);

		findViews();
		setListeners();
	}

	@Override
	public void findViews() {
		// tv_name = (TextView) findViewById(R.id.tv_name);
		// tv_name.setText("欢迎 " + userinfo.getCUSTOMERNAME());
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_up = (Button) findViewById(R.id.btn_up);
		btn_up.setVisibility(View.GONE);

		flag = getIntent().getStringExtra("flag");
		title=(TextView)findViewById(R.id.title);
		vv = (VideoView) this.findViewById(R.id.video);
		
		// 设置MediaController
		vv.setMediaController(mMediaController);
//		if (flag.equals("yunfu")) {
//			// 孕妇保健
//			uri = "android.resource://" + getPackageName() + "/" + R.raw.fetal;
////			uri = "file:///"+Environment.getExternalStorageDirectory()+"/妊娠期高血压孕妇的护理.mp4";
//			title.setText("妊娠期高血压孕妇的护理");
//		}else if(flag.equals("ertong")){
//			uri = "android.resource://" + getPackageName() + "/" + R.raw.childer;
////			uri = "file:///"+Environment.getExternalStorageDirectory()+"/0-6岁儿童中医药健康管理服务.mp4";
//			title.setText("0-6岁儿童中医药健康管理服务");
//		}else if(flag.equals("laoren")){
//			uri = "android.resource://" + getPackageName() + "/" + R.raw.oldman;
////			uri = "file:///"+Environment.getExternalStorageDirectory()+"/减盐多醋，避免高血压合并高血糖相关并发症.mp4";
//			title.setText("减盐多醋，避免高血压合并高血糖相关并发症");
//		}

		if (!uri.equals("")) {

			vv.setVideoURI(Uri.parse(uri));

			vv.start();
		}

	}

	@Override
	public void setListeners() {
		btn_back.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PlayVideoActivity.this.finish();
			}
		});
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		this.finish();

	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		return false;
	}

//	// 开始
//	public void onStart() {
//		// Play Video
//		vv.setVideoURI(Uri.parse(uri));
//		vv.start();
//
//		super.onStart();
//	}

	// 暂停

	public void onPause() {
		// Stop video when the activity is pause.
		mPositionWhenPaused = vv.getCurrentPosition();
		vv.stopPlayback();
		// Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);
		// Log.d(TAG, "OnStop: getDuration = " + vv.getDuration());

		super.onPause();
	}

	public void onResume() {
		// Resume video player
		if (mPositionWhenPaused >= 0) {
			vv.seekTo(mPositionWhenPaused);
			mPositionWhenPaused = -1;
		}

		super.onResume();
	}
	
}